package com.wu.gumomallproduct.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wu.common.entity.product.AttrEntity;
import com.wu.common.entity.product.CategoryEntity;
import com.wu.gumomallproduct.product.service.AttrService;
import com.wu.gumomallproduct.product.service.CategoryService;
import com.wu.common.vo.product.AttrGroupWithAttrsVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.common.utils.PageUtils;
import com.wu.common.utils.Query;

import com.wu.gumomallproduct.product.dao.AttrGroupDao;
import com.wu.common.entity.product.AttrGroupEntity;
import com.wu.gumomallproduct.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<AttrGroupEntity> queryWrapper = new LambdaQueryWrapper<>();
        String key = params.getOrDefault("key", "").toString();
        if (!StringUtils.isEmpty(key)){
            queryWrapper.like(AttrGroupEntity::getAttrGroupName, key);
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        if (catelogId == 0){
            return queryPage(params);
        }
        LambdaQueryWrapper<AttrGroupEntity> queryWrapper = new LambdaQueryWrapper<>();
        String key = params.getOrDefault("key", "").toString();
        queryWrapper.like(!StringUtils.isEmpty(key),AttrGroupEntity::getAttrGroupName, key);
        if (!StringUtils.isEmpty(key)){
            queryWrapper.and(x->{
                x.eq(AttrGroupEntity::getAttrGroupId, key).or().like(AttrGroupEntity::getAttrGroupName, key);
            });
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                queryWrapper.eq(AttrGroupEntity::getCatelogId, catelogId)
        );
        return new PageUtils(page);
    }

    @Override
    public AttrGroupEntity getByAttrGroupId(Long attrGroupId) {
        AttrGroupEntity attrGroupEntity = this.getById(attrGroupId);
        Long catelogId = attrGroupEntity.getCatelogId();
        List<Long> list = new ArrayList<>();
        List<Long> catelogPath = this.findCatelogPath(catelogId, list);
        attrGroupEntity.setCatelogPath(catelogPath.toArray(new Long[catelogPath.size()]));
        return attrGroupEntity;
    }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        List<AttrGroupEntity> list = this.list(new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId, catelogId));
        List<AttrGroupWithAttrsVo> collect = list.stream().map(attrGroup -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(attrGroup, attrGroupWithAttrsVo);
            List<AttrEntity> attrList = attrService.getAttrList(attrGroupWithAttrsVo.getAttrGroupId());
            attrGroupWithAttrsVo.setAttrs(attrList);
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
        return collect;
    }


    private List<Long> findCatelogPath(Long catelogId, List<Long> list) {
        CategoryEntity category = categoryService.getById(catelogId);
        if (category.getParentCid() != 0){
            list = findCatelogPath(category.getParentCid(), list);
        }
        list.add(catelogId);
        return list;
    }

}